package people.paper.tv.activity.home.modual.rmrb.view.impl;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.NewRecycleAdapter;
import android.support.v7.widget.OrientationHelper;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.skyworth.ui.newrecycleview.NewRecycleAdapterItem;
import com.skyworth.ui.newrecycleview.NewRecycleLayout;
import com.skyworth.ui.newrecycleview.OnBoundaryListener;
import com.skyworth.ui.newrecycleview.OnItemClickListener;
import com.skyworth.ui.newrecycleview.OnItemFocusChangeListener;
import com.skyworth.util.Util;

import java.util.ArrayList;
import java.util.List;

import people.paper.tv.R;
import people.paper.tv.activity.home.modual.rmrb.view.data.DateListData;

public class DatePickListView extends FrameLayout implements OnBoundaryListener,
        OnItemFocusChangeListener, OnItemClickListener {
    final String TAG = "YWYLViewImpl";
    TextView formerPaper;
    NewRecycleLayout gridView;
    List<DateListData> gridDatas;
    NewRecycleAdapter gridAdapter;
    RMRBLayout parentLayout;
    int curPosition;

    public DatePickListView(@NonNull Context context) {
        super(context);
        setClipChildren(false);
        setClipToPadding(false);
        setBackgroundColor(getResources().getColor(R.color.date_list_bg));

        formerPaper = new TextView(context);
        formerPaper.setText(R.string.former_paper);
        formerPaper.setTextSize(Util.Dpi(48));
        formerPaper.setTextColor(getResources().getColor(R.color.title_unfocus));
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.leftMargin = Util.Div(60);
        params.topMargin = Util.Div(56);
        addView(formerPaper, params);
    }

    public void setParentView(RMRBLayout layout){
        parentLayout = layout;
    }

    private void createNewsList(){
        gridView = new NewRecycleLayout(getContext(), 1, OrientationHelper.VERTICAL);
        LayoutParams gridViewParams = new LayoutParams(Util.Div(394), Util.Div(1080-160));
        gridViewParams.topMargin = Util.Div(160);
        gridView.setItemSpace(Util.Div(0),Util.Div(0));
        addView(gridView,gridViewParams);
        gridView.setMiddleScroll(true);
        gridView.setmBoundaryListener(this);
        gridView.setmItemFocusChangeListener(this);
        gridView.setmItemClickListener(this);
        gridView.setClipChildren(false);
        gridView.setClipToPadding(false);
    }

    public void refresh(List<DateListData> datas){
        gridDatas = datas;
        if(gridView == null) {
            createNewsList();
        }
        setAdapter(gridDatas);
    }

    protected void setAdapter(List<DateListData> datas){
        if(datas == null || datas.size() <= 0)
            return;
        gridDatas = new ArrayList<>();
        int size = datas.size();
        for(int i = 0; i < size; i++)
        {
            gridDatas.add(datas.get(i));
        }
        gridAdapter = new NewRecycleAdapter<DateListData>(gridDatas,1) {
            @Override
            public NewRecycleAdapterItem<DateListData> onCreateItem(Object type) {
                return new DatePickItemView(getContext());
            }
        };
        gridView.setRecyclerAdapter(gridAdapter);
        refreshCurPlayId(0);
    }


    public void refreshCurPlayId(int position){
        gridDatas.get(curPosition).isCurSelected = false;
        gridDatas.get(position).isCurSelected = true;
        gridView.notifyItemChanged(curPosition);
        gridView.notifyItemChanged(position);
        curPosition = position;
        gridView.smoothScrollToPosition(position);
    }

    public void getFocus(){
        if(gridView != null)
            gridView.post(new Runnable() {
                @Override
                public void run() {
                    if(gridView.getChildAt(0) != null) {
                        gridView.getChildAt(0).requestFocus();
                    }
                }
            });
    }

    @Override
    public boolean onLeftBoundary(View view, int i) {
        return true;
    }

    @Override
    public boolean onTopBoundary(View view, int i) {
        return true;
    }

    @Override
    public boolean onDownBoundary(View view, int i) {
        return true;
    }

    @Override
    public boolean onRightBoundary(View view, int i) {
        return parentLayout.onKey(view, KeyEvent.KEYCODE_DPAD_RIGHT,
                new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT));
    }

    @Override
    public boolean onOtherKeyEvent(View view, int i, int i1) {
        return parentLayout.onKey(view, i1, new KeyEvent(KeyEvent.ACTION_DOWN, i1));
    }

    @Override
    public void focusChange(View view, int i, boolean hasFocus) {
        DatePickItemView itemView = (DatePickItemView)view;
        itemView.onFocusChange(hasFocus);
    }

    @Override
    public void click(View v, int position) {
        DatePickItemView itemView = (DatePickItemView)v;
        itemView.onClickAction();
        refreshCurPlayId(position);
        parentLayout.showDataPick(false);
    }
}
